package com.zhangxu.microservice.system.web.convert;

import com.zhangxu.microservice.system.biz.dto.BatchUploadResourceTreeDTO;
import com.zhangxu.microservice.system.biz.dto.ResourceTreeDTO;
import com.zhangxu.microservice.system.web.request.BatchUploadResourceTreeReq;
import com.zhangxu.microservice.system.web.request.ResourceTreeReq;
import com.zhangxu.microservice.system.web.response.ResourceTreeResp;
import org.mapstruct.Mapper;

import java.util.ArrayList;
import java.util.List;

/**
 * 资源Web层对象转换接口
 */
@Mapper(componentModel = "spring")
public interface ResourceWebConverter {


    /**
     * 将 ResourceTreeDTO 转换为 ResourceTreeResp
     *
     * @param resourceTreeDTO 资源树DTO
     * @return 资源树响应
     */
    default ResourceTreeResp toResourceTreeResp(ResourceTreeDTO resourceTreeDTO) {
        if (resourceTreeDTO == null) {
            return null;
        }

        ResourceTreeResp resourceTreeResp = new ResourceTreeResp();
        resourceTreeResp.setId(resourceTreeDTO.getId());
        resourceTreeResp.setAppId(resourceTreeDTO.getAppId());
        resourceTreeResp.setCode(resourceTreeDTO.getCode());
        resourceTreeResp.setParentCode(resourceTreeDTO.getParentCode());
        resourceTreeResp.setName(resourceTreeDTO.getName());
        resourceTreeResp.setType(resourceTreeDTO.getType());
        resourceTreeResp.setPath(resourceTreeDTO.getPath());
        resourceTreeResp.setComponent(resourceTreeDTO.getComponent());
        resourceTreeResp.setMethod(resourceTreeDTO.getMethod());

        if (resourceTreeDTO.getChildren() != null) {
            List<ResourceTreeResp> children = new ArrayList<>();
            for (ResourceTreeDTO childDTO : resourceTreeDTO.getChildren()) {
                children.add(toResourceTreeResp(childDTO));
            }
            resourceTreeResp.setChildren(children);
        }

        return resourceTreeResp;
    }

    /**
     * 将 ResourceTreeDTO 列表转换为 ResourceTreeResp 列表
     *
     * @param resourceTreeDTOList 资源树DTO列表
     * @return 资源树响应列表
     */
    default List<ResourceTreeResp> toResourceTreeRespList(List<ResourceTreeDTO> resourceTreeDTOList) {
        List<ResourceTreeResp> resourceTreeRespList = new ArrayList<>();
        if (resourceTreeDTOList != null) {
            for (ResourceTreeDTO resourceTreeDTO : resourceTreeDTOList) {
                resourceTreeRespList.add(toResourceTreeResp(resourceTreeDTO));
            }
        }
        return resourceTreeRespList;
    }

    /**
     * 将 BatchUploadResourceTreeReq 转换为 BatchUploadResourceTreeDTO
     *
     * @param batchUploadResourceTreeReq 批量上传资源树请求
     * @return 批量上传资源树DTO
     */
    default BatchUploadResourceTreeDTO toBatchUploadResourceTreeDTO(BatchUploadResourceTreeReq batchUploadResourceTreeReq) {
        BatchUploadResourceTreeDTO batchUploadResourceTreeDTO = new BatchUploadResourceTreeDTO();
        batchUploadResourceTreeDTO.setAppCode(batchUploadResourceTreeReq.getAppCode());
        // 手动转换ResourceTreeReq列表到ResourceTreeDTO列表
        if (batchUploadResourceTreeReq.getResources() != null) {
            List<ResourceTreeDTO> resourceTreeDTOList = new ArrayList<>();
            for (ResourceTreeReq resourceTreeReq : batchUploadResourceTreeReq.getResources()) {
                resourceTreeDTOList.add(toResourceTreeDTO(resourceTreeReq));
            }
            batchUploadResourceTreeDTO.setResources(resourceTreeDTOList);
        }
        return batchUploadResourceTreeDTO;
    }

    default ResourceTreeDTO toResourceTreeDTO(ResourceTreeReq resourceTreeReq) {
        if (resourceTreeReq == null) {
            return null;
        }

        ResourceTreeDTO resourceTreeDTO = new ResourceTreeDTO();
        resourceTreeDTO.setCode(resourceTreeReq.getCode());
        resourceTreeDTO.setName(resourceTreeReq.getName());
        resourceTreeDTO.setType(resourceTreeReq.getType());
        resourceTreeDTO.setPath(resourceTreeReq.getPath());
        resourceTreeDTO.setComponent(resourceTreeReq.getComponent());
        resourceTreeDTO.setMethod(resourceTreeReq.getMethod());

        if (resourceTreeReq.getChildren() != null) {
            List<ResourceTreeDTO> children = new ArrayList<>();
            for (ResourceTreeReq childReq : resourceTreeReq.getChildren()) {
                children.add(toResourceTreeDTO(childReq));
            }
            resourceTreeDTO.setChildren(children);
        }

        return resourceTreeDTO;
    }
}